<?php
require_once(Config::acquire("Controller", null, "controller"));
require_once(Config::acquire("Probleem", MODEL));
require_once(Config::acquire("Diensten", MODEL));
require_once(Config::acquire("Incident", MODEL));
/**
 * Created by JetBrains PhpStorm.
 * User: Sander
 * Date: 29-6-11
 * Time: 18:45
 * To change this template use File | Settings | File Templates.
 */

class ProbleemController extends Controller
{

    public function index()
    {
        $this->view->render("Probleem.tpl");
    }

    public function overzicht()
    {
        $list = array();
        foreach (Probleem::readAll() as $probleem)
            $list[] = $probleem->toArray();
        $this->view->assign('detail_link', SDS::getURL('Probleem', 'detail'));
        $this->view->assign('gebruiker_link', SDS::getURL('Gebruiker', 'detail'));
        $this->view->assign('medewerker_link', SDS::getURL('Employee', 'detail'));
        $this->view->assign('sla_link', SDS::getURL('Sla', 'detail'));
        $this->view->assign("probleemlijst", $list);
        $this->view->render('Probleem_List.tpl');

    }

    public function overzichtOpenstaand()
    {
        $list = array();
        foreach (Probleem::readAfgehandeld() as $probleem)
            $list[] = $probleem->toArray();
        $this->view->assign('detail_link', SDS::getURL('Probleem', 'detail'));
        $this->view->assign('gebruiker_link', SDS::getURL('Gebruiker', 'detail'));
        $this->view->assign('medewerker_link', SDS::getURL('Employee', 'detail'));
        $this->view->assign('hardware_link', SDS::getURL('Hardware', 'detail'));
        $this->view->assign('sla_link', SDS::getURL('Sla', 'detail'));
        $this->view->assign("probleemlijst", $list);
        $this->view->render('Probleem_Openstaand.tpl');
    }

    public function overzichtMogelijk()
    {
        $list = array();
        $listG = array();
        foreach (Probleem::readMogelijkHardware() as $key => $probleem)
            foreach ($probleem as $incident) {
                $list[$key][] = $incident->toArray();
             //   $problem[] = $key;
            }
        foreach (Probleem::readMogelijkGebruiker() as $key => $probleem)
            foreach ($probleem as $incident) {
                $listG[$key][] = $incident->toArray();
                 //   $problem[] = $key;
        }

        $this->view->assign('form_url', SDS::getURL('Probleem', 'addProbleem'));
        $this->view->assign('fooG', $listG);
        $this->view->assign('probleemlijstG', $listG);
        $this->view->assign('foo',$list);
        $this->view->assign('detail_link', SDS::getURL('Incident', 'detail'));
        $this->view->assign('gebruiker_link', SDS::getURL('Gebruiker', 'detail'));
        $this->view->assign('medewerker_link', SDS::getURL('Employee', 'detail'));
        $this->view->assign('hardware_link', SDS::getURL('Hardware', 'detail'));
        $this->view->assign('sla_link', SDS::getURL('Sla', 'detail'));
        $this->view->assign("probleemlijst", $list);
        $this->view->render('Probleem_Mogelijk.tpl');

    }


    public function detail()
    {
        $params = UrlDecoder::get_Params();
        $incidentlijst = array();
        if (is_null($params[0]))
            SDS::redirect("Probleem", "overzicht");
        $id = $params[0];
        foreach (Probleem::read($id)->toArray() as $key => $value) {
            $this->view->assign($key, $value);
        }
        $this->incident($id);
        $this->view->assign('wijzig_url', SDS::getURL('Probleem', 'bewerken', array($params[0])));
        $this->view->assign('afhandel_url', SDS::getURL('Probleem', 'Afhandelen', array($params[0])));

        foreach (ProbleemIncident::read($id)->getIncident() as $incident)
            $incidentlijst[] = $incident->toArray();
        $this->view->assign("incidentlijst", $incidentlijst);
        $this->view->assign('verwijder_link', SDS::getURL("Probleem", "detail", array($id, 'delete')));
        $this->view->assign('toevoegen_link', SDS::getURL("Probleem", "detail", array($id, 'add')));
        $this->view->assign('detail_link', SDS::getURL("Incident", "detail"));
        $this->view->assign('gebruiker_link', SDS::getURL('Gebruiker', 'detail'));
        $this->view->assign('medewerker_link', SDS::getURL('Employee', 'detail'));
        $this->view->assign('hardware_link', SDS::getURL('Hardware', 'detail'));

        $this->view->render('Probleem_Detail.tpl');

    }

    public function toevoegen()
    {
        $this->view->assign('form_URL', SDS::getURL('Probleem', 'add', null));
        $this->view->render("Probleem_Change.tpl");
    }


    public function bewerken()
    {
        $params = UrlDecoder::get_Params();
        if (!isset($params[0]))
            SDS::redirect("Probleem", "overzicht");
        $id = $params[0];

        foreach (Probleem::read($id)->toArray() as $key => $value) {
            $this->view->assign($key, $value);
        }

        $this->view->assign('form_URL', SDS::getURL('Probleem', 'update', array($id)));
        $this->view->assign('ProbleemTitle', "Probleem wijzigen");
        $this->view->render("Probleem_Change.tpl");
    }

    public function addProbleem()
    {
        Probleem::create(null, "", 'nee', Medewerker::read(Register::get('medewerker_ID', true)), null, null)->update();
        foreach($_POST['addProbleem'] as $IDs)
        {
            ProbleemIncident::read(Probleem::returnID())->add($IDs);
        }
            SDS::redirect("Probleem", "geefOmschrijving");
    }

    public function geefOmschrijving()
    {
        $this->view->assign('form_url', SDS::getURL('Probleem', 'geefOmschrijvingAdd', null));
        $this->view->render("Probleem_Omschrijving.tpl");
    }

    public function geefOmschrijvingAdd()
    {
       $id = Probleem::returnID();
       if(!is_null($id)){
            Probleem::setProbleemOmschrijving($_POST['Omschrijving']);
            SDS::redirect("Probleem", "overzicht");
            
        }else{
            SDS::redirect("Probleem", "overzicht");
        }

    }
    public function add()
    {
        require_once(Config::acquire('FormValidation', APPLICATION));
        FormValidation::stripInput();
        Probleem::create(null,
                         $_POST['Omschrijving'],
                         'nee',
                         Medewerker::read(Register::get('medewerker_ID', true)),
                         null,
                         null)->update();
        SDS::redirect("Probleem", "overzicht");
    }

    public function delete()
    {
        return;
    }

    public function update()
    {
        require_once(Config::acquire('FormValidation', APPLICATION));
        FormValidation::stripInput();
        $params = UrlDecoder::get_Params();
        if (isset($params[0])) {
            $probleem = Probleem::read(($params[0]));
            $probleem->setOmschrijving($_POST['Omschrijving']);
            $probleem->update();
            SDS::redirect("Probleem", "detail", $params[0]);
        }
        SDS::redirect("Probleem", "overzicht", null);
    }

    public function afhandelen()
    {
        $id = UrlDecoder::get_Params();
        if (!is_null($id[0])) {
            Probleem::read($id[0])->Afhandelen();
        }
        SDS::redirect('Probleem', 'detail', array($id[0]));
    }

    public function incident($id)
    {
        $pid = $id;
        $params = UrlDecoder::get_Params();
        if (empty($params[1]) && empty($params[2]))
            return;


        if (isset($params[2])) {
            switch ($params[1]) {
                case 'delete':
                    ProbleemIncident::read($pid)->delete($params[2]);
                    SDS::redirect('Probleem', "detail", array($pid));
                    break;
                case 'add':
                    require_once(Config::acquire('FormValidation', APPLICATION));
                    FormValidation::stripInput();
                    ProbleemIncident::read($pid)->add($_POST['Incident']);
                    SDS::redirect('Probleem', "detail", array($pid));
                    break;
            }

        } else {
            $this->view->assign('incident', ProbleemIncident::read($id)->getUnique());
            $this->view->assign('form_url', SDS::getURL("Probleem", 'detail', array($pid, 'add', 5)));
        }
    }
}

